package com.wuwei.tms.aspect;

import com.wuwei.tms.pojo.Airplane;
import com.wuwei.tms.pojo.Tool;
import com.wuwei.tms.pojo.User;
import com.wuwei.tms.utils.token.TokenUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * AOP切面方法
 * 更新实体类的更新时间和更新人
 */
@Aspect
@Component
public class TimestampAspect {

    @Autowired
    private HttpServletRequest request;

    @Before("execution(* com.wuwei.tms.setting.service.AirplaneService.addAirplane(..))" +
            "|| execution(* com.wuwei.tms.setting.controller.UserController.regesiter(..))" +
            "|| execution(* com.wuwei.tms.workbench.controller.ToolController.add(..))")
    public void beforeSave(JoinPoint joinPoint) {
        String token = request.getHeader("Authorization");
        Long empno = TokenUtil.getEmpnoFromToken(token);
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            switch (arg.getClass().getSimpleName()) {
                case "User":
                    User user = (User) arg;
                    user.setCreatedBy(empno);
                    user.setCreatedTime(LocalDateTime.now());
                    break;
                case "Airplane":
                    Airplane airplane = (Airplane) arg;
                    airplane.setCreatedBy(empno);
                    airplane.setCreatedTime(LocalDateTime.now());
                    break;
                case "Tool":
                    Tool tool = (Tool) arg;
                    tool.setCreateTime(LocalDateTime.now());
                    tool.setCreateBy(empno);
                    break;
                default:
                    // 处理未知类型的情况
                    break;
            }
        }
    }

    @Before("execution(* com.wuwei.tms.setting.service.UserService.setPassword(..))" +
            "|| execution(* com.wuwei.tms.workbench.controller.ToolController.update(..))" +
            "|| execution(* com.wuwei.tms.setting.controller.AirplaneController.update(..))")
    public void beforeUpdate(JoinPoint joinPoint) {
        String token = request.getHeader("Authorization");
        Long empno = TokenUtil.getEmpnoFromToken(token);
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            switch (arg.getClass().getSimpleName()) {
                case "User":
                    User user = (User) arg;
                    user.setEditBy(empno);
                    user.setUpdatedTime(LocalDateTime.now());
                    break;
                case "Airplane":
                    Airplane airplane = (Airplane) arg;
                    airplane.setEditBy(empno);
                    airplane.setUpdatedTime(LocalDateTime.now());
                    break;
                case "Tool":

                    Tool tool = (Tool) arg;
                    tool.setEditTime(LocalDateTime.now());
                    tool.setCreateBy(empno);
                    break;
                default:
                    // 处理未知类型的情况
                    break;
            }
        }
    }
}
